'use strict';

const mybatisMapper = require('mybatis-mapper');
const path = require('path');

const mapperFileURLs = [path.join(__dirname, 'app/sql_mapper/special.xml')];

class AppBoot {
  constructor(app) {
    this.app = app;
  }

  // 配置文件即将加载
  configWillLoad() {
    console.log('configWillLoad');
  }

  // 配置文件加载完成
  configDidLoad() {
    console.log('configDidLoad');
  }

  // 文件加载完成
  didLoad() {
    console.log('didLoad');
  }

  // 插件启动完毕
  willReady() {
    console.log('willReady');

    this.app.db = this.app.mysql.createInstance(this.app.config.mysql.client);

    mybatisMapper.createMapper(mapperFileURLs);

    this.app.db.mybatisQuery = ({ namespace, sqlId, params, format }) => {
      format = Object.assign({ language: 'sql', indent: '  ' }, format);
      const sql = mybatisMapper.getStatement(namespace, sqlId, params, format);
      console.log(sql, '=sql=');
      return this.app.db.query(sql);
    };
  }

  // worker 准备就绪
  didReady() {
    console.log('didReady');
  }

  // 应用启动完成
  serverDidReady() {
    console.log('serverDidReady');
  }

  // 应用即将关闭
  beforeClose() {
    console.log('beforeClose');
  }
}

module.exports = AppBoot;
